package com.lagou.edu.filter;

import com.lagou.edu.threadlocal.TPMonitorThreadLocal;
import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.rpc.*;

/**
 * @author 赵虎
 * @date 2021/1/11 2:11 上午
 */
@Activate(group = {CommonConstants.CONSUMER})
public class TPMonitorFilter implements Filter {

    @Override
    public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
        // 记录开始时间
        long startTime = System.currentTimeMillis();
        Result invoke = invoker.invoke(invocation);
        // 记录结束时间
        long endTime = System.currentTimeMillis();
        // 存储响应时间
        String key = invocation.getServiceName() + "." + invocation.getMethodName();
        TPMonitorThreadLocal.addServiceTime(key, endTime - startTime);
        return invoke;
    }

}
